package com.data.JVM.chapter11;

/**
 * volatile关键字的测试
 */
public class VolatileTest {
    public static volatile int  race=0;

    public static void increase(){
        race++;
    }

    private static final int THREADS_COUNT =20;

    public static void main(String[] args) {
        Thread[] threads = new Thread[THREADS_COUNT];

        for (int i=0;i<THREADS_COUNT;i++){
            threads[i] = new Thread(new Runnable() {
                public void run() {
                    for (int i=0;i<10000;i++){
                        increase();
                    }
                }
            });
            threads[i].start();
            

        }

        // 线程累加结束
//        while (Thread.activeCount()>1){
//            Thread.yield();
//            System.out.println(race);
//        }

        try {
            Thread.sleep(1000);
            System.out.println(race);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
